//
//  RootTableViewController.swift
//  AlgorithmDEMO
//
//  Created by meitianhui2 on 2018/1/13.
//  Copyright © 2018年 DeLongYang. All rights reserved.
/*
    这里我们会根据不同的算法分类跳到 具体的算法的ViewController 中 
 */

import UIKit

class RootTableViewController: UITableViewController {
    
    var dataSource:NSMutableArray{
        
        // ["BinarySort","Base","Base","Base","Base",]
        return ["BinarySort","MaxCommonDivisor"]
    }
    
    let identifier = "identifier"
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataSource.count
    }
    
    // 选中某个  cell
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
        
        let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"]
        guard let ns = nameSpace as? String else{
            print("无法 转换 命名空间 ")
            return
        }
        
        let name:String = self.dataSource[indexPath.row] as! String
        let className:String = ns + "." + "\(name)Control"
        
//        let newClassName:String = "RootTableViewController"
//        let newCls:AnyClass? = NSClassFromString(newClassName)
//        guard let newClsType = newCls as? RootTableViewController.Type else {
//            print("can not change RootTableViewController ")
//            return
//        }
    
        let cls:AnyClass? = NSClassFromString(className)
        guard  let clsType = cls as? BaseControl.Type else {
            print("无法 转换成  BaseControl ")
            return
        }
        //
        let control:BaseControl = clsType.init()
        let baseVC:BaseViewController = BaseViewController.init(control: control)
        self.navigationController?.pushViewController(baseVC, animated: true)
        
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.textLabel?.text = self.dataSource[indexPath.row] as? String

        return cell
    }

}
